﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RestaurantManagement.Controls;
using WindowsFormsView;
using WindowsFormsPresenter.AccountingObjectCategory;
using WindowsFormsModel.BusinessObjects;
using DevExpress.XtraEditors;
using WindowsFormsPresenter.District;
using WindowsFormsPresenter.CountryRegion;
using WindowsFormsPresenter.Province;
using WindowsFormsPresenter.AccountingObject;

namespace RestaurantManagement.Modules.Category
{
    public partial class uCustomer : BaseModule, IAccountingObjectCategoriesView, IDistrictsView, ICountryRegionsView, IProvincesView, IAccountingObjectView, IAccountingObjectsView
    {
        #region Presenter

        private AccountingObjectCategoriesPresenter _accountingObjectCategoriesPresenter;
        private AccountingObjectPresenter _accountingObjectPresenter;
        private AccountingObjectsPresenter _accountingObjectsPresenter;
        private CountryRegionsPresenter _countryRegionsPresenter;
        private DistrictsPresenter _districtsPresenter;
        private ProvincesPresenter _provincesPresenter;

        #endregion

        #region variable

        private static int totalPage = 0;
        private static int currentPage = 0;

        private static int totalPageByCategory = 0;
        private static int currentPageByCategory = 0;

        private string accountingObjectCategoryId = null;

        #endregion

        #region Field

        public IList<AccountingObjectModel> AccountingObjects
        {
            set
            {
                var accountingObjects = value;
                AddAccountingObjectToGrid(accountingObjects);
            }
        }

        public IList<AccountingObjectCategoryModel> AccountingObjectCategories
        {
            set
            {
                var accountingObjectCategories = value;

                pnlListAccountingObject.Groups.Clear();

                TileGroup group = new TileGroup();
                foreach (var accountingObjectCategory in accountingObjectCategories)
                {
                    TileItem item = new TileItem();
                    item.BackgroundImageScaleMode = TileItemImageScaleMode.ZoomOutside;
                    item.BackgroundImageAlignment = TileItemContentAlignment.MiddleCenter;
                    item.IsLarge = true;
                    item.Padding = new Padding(0, 0, 0, 0);

                    item.Text = string.Format("<size=+1>{0}<size=-2> <br><size=+2>{1}<size=-2> ", accountingObjectCategory.AccountingObjectCategoryCode, accountingObjectCategory.AccountingObjectCategoryName);
                    item.Text3 = string.Format("<size=+1>{0}", accountingObjectCategory.Description);
                    item.TextShowMode = TileItemContentShowMode.Hover;
                    item.Tag = accountingObjectCategory.AccountingObjectCategoryID;
                    group.Items.Add(item);
                }
                pnlListAccountingObject.Groups.Add(group);

                AddAccountingObjectCategoryToCombobox(accountingObjectCategories);
            }
        }

        public IList<CountryRegionModel> CountryRegions
        {
            set
            {
                var countryRegions = value;
                AddCountryRegionToCombobox(countryRegions);
            }
        }

        public IList<DistrictModel> Districts
        {
            set
            {
                var districts = value;
                AddDistrictToCombobox(districts);
            }
        }

        public IList<ProvinceModel> Provinces
        {
            set
            {
                var provinces = value;
                AddProvinceToCombobox(provinces);
            }
        }

        public string AccountingObjectID { get; set; }

        public string AccountingObjectCode
        {
            get { return txtAccountingObjectCode.Text; }
            set { txtAccountingObjectCode.Text = value; }
        }

        public string AccountingObjectName
        {
            get { return txtAccountingObjectName.Text; }
            set { txtAccountingObjectName.Text = value; }
        }

        public string AccountingObjectCategoryId
        {
            get { return cboAccountingObjectCategoryId.EditValue.ToString(); }
            set { cboAccountingObjectCategoryId.EditValue = value; }
        }

        public string RestaurantId { get; set; }

        public string Prefix
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return null; }
                return txtPrefix.Text;
            }
            set { txtPrefix.Text = value; }
        }

        public string Address
        {
            get { return txtAddress.Text; }
            set { txtAddress.Text = value; }
        }

        public string Tel
        {
            get { return txtTel.Text; }
            set { txtTel.Text = value; }
        }

        public string Fax
        {
            get { return txtFax.Text; }
            set { txtFax.Text = value; }
        }

        public string EmailAddress
        {
            get { return txtEmailAddress.Text; }
            set { txtEmailAddress.Text = value; }
        }

        public string Website
        {
            get { return txtWebsite.Text; }
            set { txtWebsite.Text = value; }
        }

        public string CompanyTaxCode
        {
            get { return txtCompanyTaxCode.Text; }
            set { txtCompanyTaxCode.Text = value; }
        }

        public DateTime BirthDate
        {
            get { return DateTime.Parse("1/1/1975"); }
            set { var dateTime = value; }
        }

        public DateTime FiredDate
        {
            get { return DateTime.Parse("1/1/1975"); }
            set { var dateTime = value; }
        }

        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        public string ContactName
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return null; }
                return txtContactName.Text;
            }
            set { txtContactName.Text = value; }
        }

        public string JobCandidateName
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return null; }
                return txtJobCandidateName.Text;
            }
            set { txtJobCandidateName.Text = value; }
        }

        public bool ContactSex
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return true; }
                else { return false; }
            }
            set
            {
                var contactSex = value;
                if (contactSex == true) { rndContactSex.Properties.Items[0].Value = true; }
                else { rndContactSex.Properties.Items[1].Value = true; }
            }
        }

        public string ContactMobile
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return null; }
                return txtContactMobile.Text;
            }
            set { txtContactMobile.Text = value; }
        }

        public string ContactEmail
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return null; }
                return txtContactEmail.Text;
            }
            set { txtContactEmail.Text = value; }
        }

        public string ContactHomeTel
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return null; }
                return txtContactHomeTel.Text;
            }
            set { txtContactHomeTel.Text = value; }
        }

        public string ContactAddress
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return null; }
                return txtContactAddress.Text;
            }
            set { txtContactAddress.Text = value; }
        }

        public bool IsPersonal
        {
            get
            {
                if (rndIsPersonal.SelectedIndex == 1) { return true; }
                else { return false; }
            }
            set
            {
                var isPersonal = value;
                if (isPersonal == true) { rndIsPersonal.SelectedIndex = 1; }
                else { rndIsPersonal.SelectedIndex = 0; }
            }
        }

        public string IdentificationNumber
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return null; }
                return txtIdentificationNumber.Text;
            }
            set { txtIdentificationNumber.Text = value; }
        }

        public DateTime IssueDate
        {
            get
            {
                if (dtIssueDate.EditValue != null) { return dtIssueDate.DateTime; }
                else { return dtIssueDate.Properties.MinValue; }
            }
            set
            {
                if (value.ToString().Equals("1/1/1753 12:00:00 AM")) { dtIssueDate.EditValue = null; }
                else
                {
                    string[] date = value.ToString().Split('/');
                    dtIssueDate.EditValue = new DateTime(Convert.ToInt32(date[2].Substring(0, 4)), Convert.ToInt32(date[0]), Convert.ToInt32(date[1]));
                }
            }
        }

        public string IssueBy
        {
            get
            {
                if (rndContactSex.SelectedIndex == 1) { return null; }
                return txtIssueBy.Text;
            }
            set { txtIssueBy.Text = value; }
        }

        public bool IsVendor
        {
            get { return false; }
            set { var isVendor = value; }
        }

        public bool IsCustomer
        {
            get { return true; }
            set { var isCustomer = value; }
        }

        public bool IsEmployess
        {
            get { return false; }
            set { var isEmployess = value; }
        }

        public string CountryRegion
        {
            get { return cboCountryRegion.EditValue.ToString(); }
            set { cboCountryRegion.EditValue = value; }
        }

        public string Province
        {
            get { return cboProvince.EditValue.ToString(); }
            set { cboProvince.EditValue = value; }
        }

        public string District
        {
            get { return cboDistrict.EditValue.ToString(); }
            set { cboDistrict.EditValue = value; }
        }

        public string Village
        {
            get { return txtVillage.Text.ToString(); }
            set { txtVillage.Text = value; }
        }

        public bool IsActive
        {
            get { return chkIsActive.Checked; }
            set { chkIsActive.Checked = value; }
        }

        #endregion

        #region Style Button

        private void btnAddNew_MouseHover(object sender, EventArgs e)
        {
            btnAddNew.BackgroundImage = null;
            btnAddNew.BackgroundImageLayout = ImageLayout.Center;
            btnAddNew.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.them_icon_2);
        }

        private void btnAddNew_MouseLeave(object sender, EventArgs e)
        {
            btnAddNew.BackgroundImage = null;
            btnAddNew.BackgroundImageLayout = ImageLayout.Center;
            btnAddNew.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.them_con);
        }

        private void btnUpdate_MouseHover(object sender, EventArgs e)
        {
            btnUpdate.BackgroundImage = null;
            btnUpdate.BackgroundImageLayout = ImageLayout.Center;
            btnUpdate.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.sua_icon_2);
        }

        private void btnUpdate_MouseLeave(object sender, EventArgs e)
        {
            btnUpdate.BackgroundImage = null;
            btnUpdate.BackgroundImageLayout = ImageLayout.Center;
            btnUpdate.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.sua_icon);
        }

        private void btnDelete_MouseHover(object sender, EventArgs e)
        {
            btnDelete.BackgroundImage = null;
            btnDelete.BackgroundImageLayout = ImageLayout.Center;
            btnDelete.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.xoa_icon_2);
        }

        private void btnDelete_MouseLeave(object sender, EventArgs e)
        {
            btnDelete.BackgroundImage = null;
            btnDelete.BackgroundImageLayout = ImageLayout.Center;
            btnDelete.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.xoa_icon);
        }

        private void btnRefresh_MouseLeave(object sender, EventArgs e)
        {
            btnRefresh.BackgroundImage = null;
            btnRefresh.BackgroundImageLayout = ImageLayout.Center;
            btnRefresh.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.lammoi_icon);
        }

        private void btnRefresh_MouseHover(object sender, EventArgs e)
        {
            btnRefresh.BackgroundImage = null;
            btnRefresh.BackgroundImageLayout = ImageLayout.Center;
            btnRefresh.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.lammoi_icon_2);
        }

        #endregion

        private void AddCountryRegionToCombobox(IList<CountryRegionModel> Districts)
        {
            if (Districts != null)
            {
                cboCountryRegion.Properties.DataSource = Districts;
                cboCountryRegion.Properties.DisplayMember = "CountryRegionName";
                cboCountryRegion.Properties.ValueMember = "CountryRegionCode";

                cboCountryRegion.Properties.ForceInitialize();
                cboCountryRegion.Properties.PopulateColumns();
                for (int i = 0; i < cboCountryRegion.Properties.Columns.Count; i++) { cboCountryRegion.Properties.Columns[i].Visible = false; }

                cboCountryRegion.Properties.Columns["CountryRegionCode"].Visible = true;
                cboCountryRegion.Properties.Columns["CountryRegionName"].Visible = true;

                cboCountryRegion.Properties.Columns["CountryRegionCode"].Caption = "Mã quốc gia";
                cboCountryRegion.Properties.Columns["CountryRegionName"].Caption = "Tên quốc gia";

                cboCountryRegion.ItemIndex = 1;
            }
            else
            {
                cboCountryRegion.Properties.DataSource = null;
            }
        }

        private void AddDistrictToCombobox(IList<DistrictModel> districts)
        {
            if (districts != null)
            {
                cboDistrict.Properties.DataSource = districts;
                cboDistrict.Properties.DisplayMember = "DistrictName";
                cboDistrict.Properties.ValueMember = "DistrictCode";

                cboDistrict.Properties.ForceInitialize();
                cboDistrict.Properties.PopulateColumns();
                for (int i = 0; i < cboDistrict.Properties.Columns.Count; i++) { cboDistrict.Properties.Columns[i].Visible = false; }

                cboDistrict.Properties.Columns["DistrictCode"].Visible = true;
                cboDistrict.Properties.Columns["DistrictName"].Visible = true;

                cboDistrict.Properties.Columns["DistrictCode"].Caption = "Mã tỉnh, thành";
                cboDistrict.Properties.Columns["DistrictName"].Caption = "Tên tỉnh, thành";

                cboDistrict.ItemIndex = 1;
            }
            else
            {
                cboDistrict.Properties.DataSource = null;
            }
        }

        private void AddProvinceToCombobox(IList<ProvinceModel> provinces)
        {
            if (provinces != null)
            {
                cboProvince.Properties.DataSource = provinces;
                cboProvince.Properties.DisplayMember = "ProvinceName";
                cboProvince.Properties.ValueMember = "ProvinceCode";

                cboProvince.Properties.ForceInitialize();
                cboProvince.Properties.PopulateColumns();
                for (int i = 0; i < cboProvince.Properties.Columns.Count; i++) { cboProvince.Properties.Columns[i].Visible = false; }

                cboProvince.Properties.Columns["ProvinceCode"].Visible = true;
                cboProvince.Properties.Columns["ProvinceName"].Visible = true;

                cboProvince.Properties.Columns["ProvinceCode"].Caption = "Mã tỉnh, thành";
                cboProvince.Properties.Columns["ProvinceName"].Caption = "Tên tỉnh, thành";

                cboProvince.ItemIndex = 1;
            }
            else
            {
                cboProvince.Properties.DataSource = null;
            }
        }

        private void AddAccountingObjectCategoryToCombobox(IList<AccountingObjectCategoryModel> accountingObjectCategories)
        {
            if (accountingObjectCategories != null)
            {
                cboAccountingObjectCategoryId.Properties.DataSource = accountingObjectCategories;
                cboAccountingObjectCategoryId.Properties.DisplayMember = "AccountingObjectCategoryCode";
                cboAccountingObjectCategoryId.Properties.ValueMember = "AccountingObjectCategoryID";

                cboAccountingObjectCategoryId.Properties.ForceInitialize();
                cboAccountingObjectCategoryId.Properties.PopulateColumns();
                for (int i = 0; i < cboAccountingObjectCategoryId.Properties.Columns.Count; i++) { cboAccountingObjectCategoryId.Properties.Columns[i].Visible = false; }

                cboAccountingObjectCategoryId.Properties.Columns["AccountingObjectCategoryCode"].Visible = true;
                cboAccountingObjectCategoryId.Properties.Columns["AccountingObjectCategoryName"].Visible = true;

                cboAccountingObjectCategoryId.Properties.Columns["AccountingObjectCategoryCode"].Caption = "Mã loại khách hàng";
                cboAccountingObjectCategoryId.Properties.Columns["AccountingObjectCategoryName"].Caption = "Tên loại khách hàng";

                cboAccountingObjectCategoryId.ItemIndex = 1;
            }
            else
            {
                cboAccountingObjectCategoryId.Properties.DataSource = null;
            }
        }

        private void AddAccountingObjectToGrid(IList<AccountingObjectModel> accountingObjects)
        {
            if ((accountingObjects != null) && (accountingObjects.Count > 0))
            {
                grdMain.DataSource = accountingObjects;
                grdMain.ForceInitialize();

                for (int i = 0; i < grdMainView.Columns.Count; i++) { grdMainView.Columns[i].Visible = false; }

                grdMainView.Columns["AccountingObjectCode"].Visible = true;
                grdMainView.Columns["AccountingObjectName"].Visible = true;
                grdMainView.Columns["Address"].Visible = true;

                grdMainView.Columns["AccountingObjectCode"].Caption = "Mã khách hàng";
                grdMainView.Columns["AccountingObjectName"].Caption = "Tên khách hàng";
                grdMainView.Columns["Address"].Caption = "Địa chỉ";

                grdMainView.Columns["AccountingObjectCode"].VisibleIndex = 0;
                grdMainView.Columns["AccountingObjectName"].VisibleIndex = 1;
                grdMainView.Columns["Address"].VisibleIndex = 2;

                grdMainView.FocusedRowHandle = 0;
            }
            else
            {
                grdMain.DataSource = null;
                InitializeTextbox();
            }
        }

        private void InitializeTextbox()
        {
        }

        private void LoadData()
        {
            _accountingObjectCategoriesPresenter.DisplayActive(true);
            _countryRegionsPresenter.DisplayActive();
            _districtsPresenter.DisplayActive();
            _provincesPresenter.DisplayActive();
            LoadDataByPage();
        }

        private void LoadDataByPage()
        {
            try
            {
                totalPageByCategory = _accountingObjectsPresenter.GetTotalPage(true);
                _accountingObjectsPresenter.DisplayPageByBranch(currentPage, true);
                lblPage.Text = "Số trang " + (currentPage + 1) + "/ " + (totalPage + 1);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void LoadDataByPageAndCategory(string accountingObjectCategoryId)
        {
            try
            {
                totalPage = _accountingObjectsPresenter.GetTotalPageByCategory(accountingObjectCategoryId);
                _accountingObjectsPresenter.DisplayPageByBranchAndCategory(currentPageByCategory, accountingObjectCategoryId);
                lblPage.Text = "Số trang " + (currentPageByCategory + 1) + "/ " + (totalPageByCategory + 1);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public uCustomer()
        {
            InitializeComponent();
            pnlListAccountingObject.ItemSize = 120;
            int width = pnlListAccountingObject.ItemSize * 3 / 2;
            ((ITileControl)pnlListAccountingObject).Properties.LargeItemWidth = width;
            pnlListAccountingObject.Width = width + pnlListAccountingObject.Padding.Horizontal + SystemInformation.VerticalScrollBarWidth + 2;

            _accountingObjectCategoriesPresenter = new AccountingObjectCategoriesPresenter(this);
            _countryRegionsPresenter = new CountryRegionsPresenter(this);
            _districtsPresenter = new DistrictsPresenter(this);
            _provincesPresenter = new ProvincesPresenter(this);
            _accountingObjectPresenter = new AccountingObjectPresenter(this);
            _accountingObjectsPresenter = new AccountingObjectsPresenter(this);
        }

        private void uCustomer_Load(object sender, EventArgs e)
        {
            try
            {
                LoadData();
                LoadDataByPage();
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void rndGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rndIsPersonal.SelectedIndex == 0) { tabMain.TabPages[1].PageVisible = true; }
            else { tabMain.TabPages[1].PageVisible = false; }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(AccountingObjectCode)) || (string.IsNullOrEmpty(AccountingObjectName)) || (string.IsNullOrEmpty(AccountingObjectCategoryId)))
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            for (int i = 0; i < grdMainView.RowCount; i++)
            {
                if (txtAccountingObjectCode.Text == grdMainView.GetRowCellValue(i, "AccountingObjectCode").ToString())
                {
                    XtraMessageBox.Show("Mã khách hàng đã tồn tại trong hệ thống !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtAccountingObjectCode.Focus();
                    return;
                }
            }

            try
            {
                AccountingObjectID = null;
                _accountingObjectPresenter.Save();
                LoadDataByPage();
                XtraMessageBox.Show("Thêm mới dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(AccountingObjectCode)) || (string.IsNullOrEmpty(AccountingObjectName)) || (string.IsNullOrEmpty(AccountingObjectCategoryId)))
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (txtAccountingObjectCode.Text != grdMainView.GetFocusedRowCellValue("AccountingObjectCode").ToString())
            {
                XtraMessageBox.Show("Bạn không được phép sửa mã số khách hàng !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAccountingObjectCode.Text = grdMainView.GetFocusedRowCellValue("AccountingObjectCode").ToString();
                return;
            }

            try
            {
                _accountingObjectPresenter.Save();
                LoadDataByPage();
                XtraMessageBox.Show("Sửa dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try { LoadData(); InitializeTextbox(); }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void picPreviousPage_Click(object sender, EventArgs e)
        {
            if (currentPage > 0)
            {
                currentPage = currentPage - 1;
                LoadDataByPage();
            }

            if (currentPageByCategory > 0)
            {
                currentPageByCategory = currentPageByCategory - 1;
                LoadDataByPageAndCategory(accountingObjectCategoryId);
            }
        }

        private void picNextPage_Click(object sender, EventArgs e)
        {
            if (currentPage < totalPage)
            {
                currentPage = currentPage + 1;
                LoadDataByPage();
            }

            if (currentPageByCategory < totalPageByCategory)
            {
                currentPageByCategory = currentPageByCategory + 1;
                LoadDataByPageAndCategory(accountingObjectCategoryId);
            }
        }

        private void grdMainView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                if (grdMainView.CalcHitInfo(e.Location).InRow && grdMainView.FocusedRowHandle >= 0)
                {
                    var accountingObject = (AccountingObjectModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    if (accountingObject != null) { _accountingObjectPresenter.Display(accountingObject.AccountingObjectID); }
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void grdMainView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (grdMainView.FocusedRowHandle >= 0)
                {
                    var accountingObject = (AccountingObjectModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    if (accountingObject != null) { _accountingObjectPresenter.Display(accountingObject.AccountingObjectID); }
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdMainView.FocusedRowHandle >= 0)
                {
                    var accountingObject = (AccountingObjectModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    new AccountingObjectPresenter(null).Delete(accountingObject.AccountingObjectID);
                    LoadDataByPage();
                    XtraMessageBox.Show("Xóa dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    XtraMessageBox.Show("Bạn chưa chọn dòng để xóa !", "Nhà hàng Hương Việt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void pnlListAccountingObject_ItemClick(object sender, TileItemEventArgs e)
        {
            try
            {
                accountingObjectCategoryId = e.Item.Tag.ToString();

                LoadDataByPageAndCategory(accountingObjectCategoryId);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }
}